package com.suixun.queue;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.suixun.dao.AdminoperationlogMapper;
import com.suixun.entity.Adminoperationlog;

/*
 * 监听 找手表 录音命令 listenin find audio
 */
public class DBUpdateLogDeal extends BaseDeal {

    private static ConcurrentLinkedQueue<Adminoperationlog> queue =
            new ConcurrentLinkedQueue<Adminoperationlog>();

    public static void add(Adminoperationlog log) {
        queue.offer(log);
    }

    @Resource
    private AdminoperationlogMapper adminoperationlogMapper;

    protected static Log logger = LogFactory.getLog(DBUpdateLogDeal.class);

    public static void main(String[] args) {
        String sql =
                "update std_people_object SET familystatus = '妈妈' WHERE ( peopleid = 9193 and objectid = 545 and isdelete = false )";
        Integer peopleid = 0;
        Integer objectid = 0;
        String pattern = "peopleid\\s*=\\s*(\\d+)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(sql);
        if (m.find()) {
            peopleid = new Integer(m.group(1));
        }
        pattern = "objectid\\s*=\\s*(\\d+)";
        r = Pattern.compile(pattern);
        m = r.matcher(sql);
        if (m.find()) {
            objectid = new Integer(m.group(1));
        }
    }

    public void execute() throws InterruptedException {
        while (true) {
            try {
                if (!queue.isEmpty()) {
                    Adminoperationlog log = queue.poll();
                    if (log != null) {
                        
                        adminoperationlogMapper.insert(log);
                    } else {
                        Thread.sleep(1000);
                    }
                } else {
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                logger.error(e);
                Thread.sleep(5000);
                continue;
            }
        }
    }
}
